<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CopartnerApply extends Model
{
    public static $error = '';
    protected $fillable = ['user_id','true_name','phone','idcardnum','shop_name','shop_address'];

    /**
     * 申请成为合伙人
     */
    public function copartner_apply($postData)
    {
        $query = CopartnerApply::query()
            ->where('user_id','=',$postData['user_id']);
        $query1 = clone $query;
        $copartner_model = $query1
            ->where('status','=','1')
            ->first();

        if(!empty($copartner_model)){
            self::$error = '你已经成为合伙人，请勿重复申请';
            return false;
        }
        $query = clone $query;
        $copartner_model = $query
            ->where('status','=','0')
            ->first();
        if(!empty($copartner_model)){
            self::$error = '请勿重复申请';
            return false;
        }
        if(!self::query()->create($postData)){
            self::$error = '申请失败，请稍后再试';
            return false;
        }
        return true;
    }

    #获取合伙人 根据用户id
    public static function getPartnerInfoByUserId($uid)
    {
        $data = self::where('user_id',$uid)->where('status',1)->first();
        if (!empty($data)){
            $data = get_object_vars($data);
        }else{
            return $data;
        }
        return $data;
    }
}
